//
//  ASPopupView.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/27.
//

#import "ASPopupView.h"
#import "ASWebController.h"

#define BTNSIZE CGSizeMake(109, 0)
#define AGREEPROTOCOL @"AGREEPROTOCOL"

@implementation ASPopupView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = BACKGROUND_COLOR;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 12.0f;
        [self addSubview:self.headLab];
        [self addSubview:self.contentText];
        [self addSubview:self.lineView];
        [self addSubview:self.lineView2];
        [self addSubview:self.sureBtn];
        [self addSubview:self.noBtn];
        [self setAllViewLayout];
    }
    return self;
}

- (void)setAllViewLayout{
    [self.headLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(26);
    }];
    
    [self.contentText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(18);
        make.right.mas_equalTo(-18);
        make.top.mas_equalTo(68);
    }];
    
    @weakify(self);
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-45);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.mas_equalTo(self.lineView.mas_top);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.width.mas_equalTo(0.5);
        make.bottom.mas_equalTo(0);
    }];
        
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.mas_equalTo(self.lineView.mas_bottom);
        make.right.mas_equalTo(-20);
        make.left.mas_equalTo(self.lineView2.mas_right);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.noBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.mas_equalTo(self.sureBtn.mas_top);
        make.left.mas_equalTo(20);
        make.bottom.mas_equalTo(0);
        make.right.mas_equalTo(self.lineView2.mas_left);
    }];
}

- (UIView *)lineView{
    if (!_lineView) {
        _lineView = [UIView new];
        _lineView.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    }
    return _lineView;
}

- (UIView *)lineView2{
    if (!_lineView2) {
        _lineView2 = [UIView new];
        _lineView2.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    }
    return _lineView2;
}

- (UILabel *)headLab{
    if (!_headLab) {
        _headLab = [UILabel new];
        _headLab.textColor = [UIColor colorWithHexString:@"#1a1a1a"];
        _headLab.font = [UIFont boldSystemFontOfSize:18];
        _headLab.textAlignment = NSTextAlignmentCenter;
        _headLab.text = @"个人隐私保护指引";
    }
    return _headLab;
}

- (YYLabel *)contentText{
    if (!_contentText) {
        _contentText = [YYLabel new];
        _contentText.numberOfLines = 0;
        _contentText.preferredMaxLayoutWidth = self.frame.size.width-(18*2);
        UIColor *color = [UIColor colorWithHexString:@"#4D4D4D"];
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15], NSForegroundColorAttributeName: color};
        
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:5];
        NSString *contentStr = @"欢迎使用头像秀秀！我们非常注重保护用户个人信息及个人隐私，请您使用前详阅《用户协议》及《隐私政策》各条款信息，我们会根据您使用具体功能的需要，收集必要的用户信息。\n为了保证您能正常的使用壁纸社区相关功能，可能会分别向您请求以下权限，如你同意，请点击“同意”开始接受我们的服务。";
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:contentStr attributes:attributes];
        [text addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [contentStr length])];

        [text setTextHighlightRange:[[text string] rangeOfString:@"《用户协议》"] color:MAIN_COLOR backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            [self clickPushIfPrivacy:YES];
        }];
        
        [text setTextHighlightRange:[[text string] rangeOfString:@"《隐私政策》"] color:MAIN_COLOR backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            [self clickPushIfPrivacy:NO];
        }];
        _contentText.attributedText = text;
    }
    return _contentText;
}

- (UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [UIButton new];
        [_sureBtn setTitle:@"同意" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:MAIN_COLOR forState:UIControlStateNormal];
        _sureBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        _sureBtn.tag = 1;
        [_sureBtn addTarget:self action:@selector(clickSubBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

- (UIButton *)noBtn{
    if (!_noBtn) {
        _noBtn = [UIButton new];
        [_noBtn setTitle:@"不同意" forState:UIControlStateNormal];
        _noBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_noBtn setTitleColor:[UIColor colorWithHexString:@"#CCCCCC"] forState:UIControlStateNormal];
        _noBtn.tag = 2;
        [_noBtn addTarget:self action:@selector(clickSubBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _noBtn;
}

- (UIButton *)shadowBtn{
    if (!_shadowBtn) {
        _shadowBtn = [UIButton new];
        _shadowBtn.backgroundColor = [UIColor blackColor];
        _shadowBtn.alpha = 0.4f;
    }
    return _shadowBtn;
}

//按钮的点击事件
- (void)clickSubBtn:(UIButton *)btn{
    if (btn.tag == 1) {
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:AGREEPROTOCOL];
        [[NSUserDefaults standardUserDefaults]synchronize];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"ClickPopoutViewSureBtn" object:nil];
    }else if(btn.tag == 2){
        //退出软件
        [self exitApplication];
    }
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0;
        self.shadowBtn.alpha = 0;
    } completion:^(BOOL finished) {
        [self.shadowBtn removeFromSuperview];
        [self removeFromSuperview];
    }];
}

- (void)exitApplication {
    exit(0);
}

- (void)hiddenView{
    self.hidden = YES;
    self.shadowBtn.hidden = YES;
}

//创建背景的半透明阴影图
- (void)createShadowBtn{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self.shadowBtn];
    [self.shadowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (void)clickPushIfPrivacy:(BOOL)IfPrivacy{
    ASWebController *webVC = [ASWebController new];
    webVC.hidesBottomBarWhenPushed = YES;
    if(IfPrivacy){
        webVC.loadUrl = WEB_PROTOCOL_URL;
        webVC.title = @"用户协议";
    }else{
        webVC.loadUrl = WEB_PRIVACY_URL;
        webVC.title = @"隐私政策";
    }
    [self.superViewController.navigationController pushViewController:webVC animated:YES];
    [self hiddenView];

    webVC.backBlock = ^{
        self.hidden = NO;
        self.shadowBtn.hidden = NO;
    };
}

//创建方法
+ (void)showInViewWithSuperView:(UIViewController *)superView{
//    NSNumber *num = [[NSUserDefaults standardUserDefaults]objectForKey:AGREEPROTOCOL];
//    if (num.boolValue == NO) {
         if (superView) {
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
             ASPopupView *popView = [[ASPopupView alloc]initWithFrame:CGRectMake(0, 0, 295, 360)];
            popView.center = CGPointMake(superView.view.bounds.size.width/2, superView.view.bounds.size.height/2);
            [popView createShadowBtn];
            popView.superViewController = superView;
            [window addSubview:popView];
        }
//    }
}

@end
